<template>
  <div class="car-info">
    <div class="isShow" @click="handleShowActive()">
      {{ isShowActive ? '>' : '<' }}
    </div>
    <div class="info" ref="infoRef">
      <div class="car-title">
        <template v-for="(titleItem, index) in cartitleData" :key="titleItem">
          <span
            class="car-title-item"
            :class="{ isActive: isBtnTitleActive === index }"
            @click="handleBtnTitle(index)"
          >
            {{ titleItem }}
          </span>
        </template>
      </div>
      <color-choose class="color-choose" @handleChooseColor="handleCar3D" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import colorChoose from '@/components/colorChoose/colorChoose.vue'
import type { IChinaColor } from '@/utils/chinaColor'
import { carBody, carBody2 } from '../3D/carMaterial'

const isShowActive = ref(false)
const infoRef = ref<HTMLElement | null>(null)
const handleShowActive = () => {
  isShowActive.value = !isShowActive.value
  if (isShowActive.value) {
    infoRef.value!.style.width = '230px'
  } else {
    infoRef.value!.style.width = '0px'
  }
}

const cartitleData: string[] = ['车身1', '车身2']
const isBtnTitleActive = ref(0)
const handleBtnTitle = (titleIndex: number) => {
  isBtnTitleActive.value = titleIndex
}

const handleCar3D = (item: IChinaColor) => {
  switch (isBtnTitleActive.value) {
    case 0:
      carBody.color.set(item.hex)
      break
    case 1:
      carBody2.color.set(item.hex)
      break
  }
}
</script>

<style lang="less" scoped>
.car-info {
  position: fixed;
  top: 0;
  right: 0;
  display: flex;
  height: 100%;
  flex-direction: row;
  align-items: center;

  .isShow {
    width: 20px;
    height: 40px;
    line-height: 38px;
    text-align: center;
    font-weight: bolder;
    color: white;
    background-color: rgba(0, 0, 0, 0.1);
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
    box-shadow: -2px 0 5px rgba(255, 255, 255, 0.3);
    z-index: 100;
    user-select: none;
  }
  .info {
    width: 0px;
    height: inherit;
    background-color: rgba(0, 0, 0, 0.1);
    box-shadow: -2px 0 5px rgba(255, 255, 255, 0.3);
    z-index: 99;
    transition: all 1s ease-in-out;
    overflow: hidden;
    padding: 10px 5px;
  }
  .color-choose {
    width: 230px;
    height: calc(100% - 40px);
  }
}

.car-title {
  width: 230px;
  height: 40px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  & .isActive {
    background-color: orange;
  }
  .car-title-item {
    width: 50px;
    height: 25px;
    line-height: 25px;
    margin-right: 10px;
    text-align: center;
    border: 2px solid rgba(255, 255, 255, 0.3);
    font-size: 12px;
    color: white;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
    user-select: none;
    border-radius: 5px;
  }
}
</style>
